package com.kgc.dao;

import com.kgc.entity.Car;
import com.kgc.entity.UserCar;
import com.kgc.entity.UserRoom;
import com.kgc.utils.PageUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 车位使用者
 */
public class UserCarDao extends BaseDao {

    /**
     * 查询房屋使用者
     *
     * @param carId
     * @return
     */
    public List<UserCar> selectUserByCar(PageUtils pageUtils, String carId) {
        List<UserCar> list = new ArrayList<>();
        try {
            String sql = "select uc.*,u.username from user_car uc inner join user u on uc.user_id = u.id  where uc.car_id=?  limit ?,?";
            ResultSet rs = super.getExecuteQuery(sql, carId, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new UserCar(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4), rs.getString(5), rs.getString(6)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }

    /**
     * 查询车位使用者
     *
     * @param userId
     * @return carName List
     */
    public List<String> selectUserByUserId(Integer userId) {
        List<String> list = new ArrayList<>();
        try {
            String sql = "select c.name from user_car  uc inner join car c on uc.car_id = c.id where uc.user_id = ? and uc.status = 1";
            ResultSet rs = super.getExecuteQuery(sql, userId);
            while (rs.next()) {
                list.add(rs.getString(1));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }


    /**
     * 查询房屋总数
     *
     * @param carId
     * @return
     */
    public int totalCount(String carId) {
        try {
            String sql = "select count(*) from user_car uc inner join user u on uc.user_id = u.id  where uc.car_id=?";
            ResultSet rs = super.getExecuteQuery(sql, carId);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }

    public int stopCar(String id, String car) {
        String sql1="DELETE FROM user_car WHERE user_id = ? and car_id = ( SELECT id FROM car WHERE `name` = ?)";
        int i = super.getExecuteUpdate(sql1, id, car);
        if(i>0){
            String sql2="UPDATE `car` SET  `status` = 0 WHERE `name` = ? ";
           return super.getExecuteUpdate(sql2,car);
        }
        return 0;

    }

    public List<Car> selectCar(int type) {
        List<Car> list = new ArrayList<>();
        try {
            String sql = "select * from car where `status` = 0 and type = ?";
            ResultSet rs = super.getExecuteQuery(sql, type);
            while (rs.next()) {
                list.add(new Car(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }

    public int updateCarStatus(int id) {
        String sql = "UPDATE `car` SET `status` = 1 WHERE `id` = ?";
        return super.getExecuteUpdate(sql, id);
    }

    public int addUserCar(UserCar userCar) {
        String sql = "INSERT INTO `user_car`( `user_id`, `car_id`, `inTime`, `outTime`, `status`) VALUES (?, ?, null, null, 1)";
        return super.getExecuteUpdate(sql, userCar.getUserId(), userCar.getCarId());

    }
}
